<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;

class Message extends Controller
{
    public function index($id)
    {
        // 查询该用户所有消息
        $info = Db::name('inner_mail')->where('receive_id', $id)->select();
        // 统计未读消息
        $num = Db::name('inner_mail')->where('receive_id', $id)->count('read_flag', 0);
        // 查询该用户所有聊天对象对应的未读消息数目
//        $userList = Db::name('inner_mail')->field('count(id) as total,send_id')->where('read_flag',0)->where('receive_id',$id)->group('send_id')->select();

        // 查询该用户所有聊天对象的消息数目和对象名称(系统消息对象名称为null)
        $userList = Db::name('inner_mail')
            ->alias('m')
            ->field('sum(read_flag) as total,send_id,nickname')// 统计未读消息数
//            ->where('read_flag', 0)
            ->where('receive_id', $id)
            ->group('send_id')
            ->join('user u', 'm.send_id=u.id', 'left')
            ->select();


        return view('message/index', ['num' => $num, 'info' => $info, 'id' => $id, 'userList' => $userList]);

    }

    /**
     * 将用户发送数据存入数据库,且类型为2(用户消息)
     * @return \think\response\Json
     */
    public function update()
    {
        $data = Request::instance()->post();
        // 完善数据
        $data['send_time'] = time();
        // 消息类型为用户信息
        $data['message_type'] = 2;

        $res = Db::name('inner_mail')->insert($data);
        if ($res) {
            $data['status'] = true;
        } else {
            $data['status'] = false;
        }
        return json($data);

    }

    // A用户点击来自B用户的未读消息,将B用户发送给A的全部消息标为已读
    public function readchange()
    {
        $res = Request::instance()->post();
        // 将消息变为已读
        $data['read_flag'] = 0;

        $res = Db::name('inner_mail')->where('receive_id', $res['receive_id'])->where('send_id', $res['send_id'])->update($data);
        return json(true);
    }

    // 获取当前用户和所选用户所有对话记录

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function find()
    {
        $one = Request::instance()->post();

        $another['receive_id'] = $one['send_id'];
        $another['send_id'] = $one['receive_id'];

        $info = Db::name('inner_mail')->where(function ($query) use ($one) {
            $query->where($one);
        })->whereOr(function ($query) use ($another) {
            $query->where($another);
        })->order('send_time desc')->limit(8)->select();  // 将聊天记录按时间排序
        return json($info);
    }

    /**
     * [refresh 查询用户未读消息个数]
     * @param  string   $id             [用户id]
     * @return json    ['num'=>$res]    [消息数目num]
     */
    public function refresh($id)
    {
        // 查询该用户所有聊天对象的未读消息数目和对象名称(系统消息对象名称为null)
        $userList = Db::name('inner_mail')
            ->alias('m')
            ->field('count(read_flag) as total,send_id,nickname,message,send_time,m.id as id')
            ->where('read_flag', 1)
            ->where('receive_id', $id)
            ->group('send_id')
            ->order('send_time asc')
            ->join('user u', 'm.send_id=u.id', 'left')
            ->select();

        return json($userList);
    }

    /**
     * 将本条消息标记为已读
     * @return \think\response\Json
     */
    public function mark()
    {
        $data = Request::instance()->post();
        $id = $data['id'];
        $res = Db::name('inner_mail')->where('id',$id)->update(['read_flag'=>0]);
        return json(['status'=>true]);

    }

}
